<?php
/**
 * Created by IntelliJ IDEA.
 * User: xuewl
 * Date: 2017/12/11
 * Time: 22:10
 */
namespace app\goods\model;
use \app\common\library\Model;

class Goods extends Model
{
    protected $type = [
//        'album' => 'json',
//        'attrs' => 'json',
    ];

    protected $append = [
        'type_name',
        'thumb'
    ];


    public function getThumbAttr($value, $data) {
        $alunm = $data['album'] ? explode(',', $data['album']) : [];
        return is_array($alunm) && !empty($alunm) ? $alunm[0] : '';
    }

    public function getTypeNameAttr($value, $data) {
        return ($data['type']) ? '实物' : '虚拟';
    }

    public function setAttrsAttr($value, $data) {
        $result = [];
        if($value) {
            foreach ($value['field'] AS $k => $v) {
                if(empty($value['value'][$k])) continue;
                $result[] = [
                    'key' => $v,
                    'val' => $value['value'][$k],
                ];
            }
        }
        return \GuzzleHttp\json_encode($result);
    }

    public function getAttrsAttr($value, $data) {
        return $value ? \GuzzleHttp\json_decode($value, true) : [];
    }

    public function getAlbumAttr($value, $data) {
        return $value ? explode(',', $value) : [];
    }


    public function stockInfo() {
        return $this->hasOne("app\\goods\\model\\GoodsStock", 'id', 'stock_id');
    }



}
